package descentFly.controller
{
	import UIAway.uif_view;
	
	import UIFree.ObjectEvent;
	
	import away3d.animators.data.NullAnimation;
	
	import descentFly.app.radar.df_radarEvent;
	import descentFly.chips.df_chip;
	import descentFly.df_optionsObject;
	import descentFly.df_space;
	import descentFly.df_spaceObject;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import jiglib.physics.RigidBody;
	
	public class FlyController extends df_optionsObject {
		
		public static const FIELD_INIT: String = 'init';
/*		public static var ACCEL			: Number = 1;
		public static var TORMOS		: Number = 0.99;
		
		public static var MAX_ROLL		: Number = 1;
		public static var ACCEL_ROLL	: Number = 0.5;
		public static var TORMOS_ROLL	: Number = 0.5;
		public static var INERTIA_ROLL	: Number = 0.2;*/

		protected var _body			: df_chip;
		private var _stage			: Stage;
		private var _dispatcher		: EventDispatcher;
		private var _space			: df_space;
		
		protected var _rotateRequest	: Vector3D;
		protected var _moveRequest		: Number;
		protected var _slideRequest		: Number;
		protected var _mk				: Number;
		protected var _timer			: Timer;
		private var _keyState			: Object;
		
		
		public function FlyController(a_stage			: Stage,
									  a_space			: df_space,
									  dispatchTarget	: EventDispatcher=null,
										a_options		: Object=null) {
			_stage		= a_stage;
			_space		= a_space;
			_dispatcher	= dispatchTarget?dispatchTarget:a_stage;
			_mk			= 1;
			super(a_options);
			initialize();
		}
		
		protected function initialize(): void {
			_keyState		= new Object();
			_rotateRequest 	= new Vector3D();
			_moveRequest 	= 0;
			_slideRequest	= 0;
			_timer			= df_spaceObject.createTimer(60 * 2, doPFrame);
			_timer.start();
		}
		
		public function get keyState(): Object {
			return _keyState;
		}
		
		protected function get stage(): Stage {
			return _stage;
		}
		
		protected function get Space(): df_space {
			return _space;
		}
		
		protected function get dispatcher(): EventDispatcher {
			return _dispatcher;
		}
		
		override protected function listenEvents(): void {
			//_stage.addEventListener(Event.ENTER_FRAME, doFrame);
		}
		
		override protected function removeEvents(): void {
//			_stage.removeEventListener(Event.ENTER_FRAME, doFrame);
			_timer.removeEventListener(TimerEvent.TIMER, doPFrame);
		}
		
		public function setBody(a_body: df_chip): void {
			_body = a_body;
			resetStartPosition();
			playSound(FIELD_INIT, _body);
		}
		
		protected function resetStartPosition(): void {
			
		}
		
		public function get body(): df_chip {
			return _body;
		}
		
		protected function applyMoveRequest(): void {
			if (_body) _body.moveTo(_moveRequest * _mk);
		}
		
		protected function applySlideRequest(): void {
			if (_body) _body.slideTo(_slideRequest * _mk);
		}
		
		protected function applyRotateRequest(): void {
			if (_body) {
				_body.rollX(_rotateRequest.x * _mk);
				_body.rollY(_rotateRequest.y * _mk);
				_body.rollZ(_rotateRequest.z * _mk);
			}
		}
		
		protected function round(value: Number): Number {
			return Math.round(value * 1000) / 1000;
		}
		
		private function doPFrame(e: Event): void {
			if (!Space.disableScene) {
				doFrame();
			}
		}
		
		protected function doFrame(): void {
			calculate();
			
			applyRotateRequest();
			applyMoveRequest();
			applySlideRequest();
			
			_rotateRequest.x = round(_rotateRequest.x);
			_rotateRequest.y = round(_rotateRequest.y);
			_rotateRequest.z = round(_rotateRequest.z);
/*			
			_rotateRequest.x = _rotateRequest.x * INERTIA_ROLL; 
			_rotateRequest.y = _rotateRequest.y * INERTIA_ROLL;
			_rotateRequest.z = _rotateRequest.z * INERTIA_ROLL;*/
		}

		protected function calculate(): void {
			if (Space.cycleTime)
				_mk = Space.cycleTime / df_space.NORMAL_CYCLE_TIME;
		}
		
		
		public function typeFromRadar(): int {
			return df_radarEvent.TYPE_UNKNOWN;
		} 
	}
}